<?php

/**
 * This is the model class for table "{{dpts}}".
 *
 * The followings are the available columns in table '{{dpts}}':
 * @property string $dpt_id
 * @property string $dpt_code
 * @property string $dpt_unit
 * @property string $dpt_serial
 * @property string $dpt_name
 * @property string $dpt_admin
 * @property integer $dpt_level
 * @property string $dpt_parent
 * @property string $dpt_remark
 * @property string $created_at
 * @property string $updated_at
 */
class Dpt extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Dpt the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{dpts}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('dpt_unit, dpt_code, dpt_name', 'required'),
			array('dpt_level', 'numerical', 'integerOnly'=>true),
			array('dpt_code, dpt_admin', 'length', 'max'=>20),
			array('dpt_unit, dpt_parent', 'length', 'max'=>10),
			array('dpt_serial, dpt_remark', 'length', 'max'=>255),
			array('dpt_name', 'length', 'max'=>100),
			array('created_at, updated_at', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('dpt_id, dpt_code, dpt_unit, dpt_serial, dpt_name, dpt_admin, dpt_level, dpt_parent, dpt_remark, created_at, updated_at', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'dpt_id' => 'Dpt',
			'dpt_code' => 'Dpt Code',
			'dpt_unit' => 'Dpt Unit',
			'dpt_serial' => 'Dpt Serial',
			'dpt_name' => 'Dpt Name',
			'dpt_admin' => 'Dpt Admin',
			'dpt_level' => 'Dpt Level',
			'dpt_parent' => 'Dpt Parent',
			'dpt_remark' => 'Dpt Remark',
			'created_at' => 'Created At',
			'updated_at' => 'Updated At',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('dpt_id',$this->dpt_id,true);
		$criteria->compare('dpt_code',$this->dpt_code,true);
		$criteria->compare('dpt_unit',$this->dpt_unit,true);
		$criteria->compare('dpt_serial',$this->dpt_serial,true);
		$criteria->compare('dpt_name',$this->dpt_name,true);
		$criteria->compare('dpt_admin',$this->dpt_admin,true);
		$criteria->compare('dpt_level',$this->dpt_level);
		$criteria->compare('dpt_parent',$this->dpt_parent,true);
		$criteria->compare('dpt_remark',$this->dpt_remark,true);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('updated_at',$this->updated_at,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 保存前初始化数据。
	 * @see CActiveRecord#beforeSave()
	 */
	protected function beforeSave()
	{
		if (empty($this->dpt_parent))
		{
			$this->dpt_level = 1;
			$this->dpt_serial = serialize(array(0=>$this->dpt_unit));
		}
		else
		{
			$parent = self::findByPk($this->dpt_parent);
			$this->dpt_level = $parent->dpt_level + 1;
			$parentdptserial = unserialize($parent->dpt_serial);
			$parentdptserial[$parent->dpt_level] = $this->dpt_parent;
			$this->dpt_serial = serialize($parentdptserial);
		}
		if($this->isNewRecord)
		{
			$this->created_at = date('Y-m-d H:i:s');
			$this->updated_at = date('Y-m-d H:i:s');
		}
		else
		{
			$this->updated_at = date('Y-m-d H:i:s');
		}
		return true;
	}
	
	public function getDpts($dptserial=NULL, $returntype=1, $dptid)
	{
		if (isset($dptserial) && $dptserial != '')
		{
			$dptlist = unserialize($dptserial);
			switch ($returntype) {
				case 1: {
					$dptstring = '';
					foreach ($dptlist as $key=>$value)
					{
						if (empty($dptstring))
							$dptstring = Unit::model()->findByPk($value)->unit_name;
						else {
							$dptstring .= '-'.self::findByPk($value)->dpt_name;
						}
					}
					if (!empty($dptid))
						$dptstring .= '-'.self::findByPk($dptid)->dpt_name;
					return $dptstring;
				}
				case 2: {
					
				}
				case 3: {
					
				}
				default: {
					
				}
			}
		}
	}
}